package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.Graduate;
import org.zwy.gradetrack.service.GraduateService;

import java.util.List;

/**
 * 毕业生管理控制器
 */
@RestController
@RequestMapping("/api/graduates")
public class GraduateController {

    @Autowired
    private GraduateService graduateService;

    /**
     * 分页查询毕业生列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<Page<Graduate>>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer majorId) {
        Page<Graduate> page = new Page<>(current, size);
        return ResponseEntity.ok(Result.success(graduateService.pageGraduates(current, size, name, majorId)));
    }

    /**
     * 获取所有毕业生
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<List<Graduate>>> list() {
        return ResponseEntity.ok(Result.success(graduateService.getAllGraduates()));
    }

    /**
     * 创建毕业生
     */
    @PostMapping
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> create(@RequestBody Graduate graduate) {
        try {
            return ResponseEntity.ok(Result.success(graduateService.createGraduate(graduate)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 更新毕业生
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> update(@PathVariable Integer id, @RequestBody Graduate graduate) {
        try {
            graduate.setId(id);
            return ResponseEntity.ok(Result.success(graduateService.updateGraduate(graduate)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 删除毕业生
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> delete(@PathVariable Integer id) {
        try {
            return ResponseEntity.ok(Result.success(graduateService.deleteGraduate(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 获取毕业生详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<Graduate>> getById(@PathVariable Integer id) {
        try {
            return ResponseEntity.ok(Result.success(graduateService.getGraduateById(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(404, e.getMessage()));
        }
    }

    /**
     * 根据姓名查询毕业生
     */
    @GetMapping("/search/name")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<List<Graduate>>> getByName(@RequestParam String name) {
        return ResponseEntity.ok(Result.success(graduateService.getGraduatesByName(name)));
    }

    /**
     * 根据专业ID查询毕业生
     */
    @GetMapping("/search/major/{majorId}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<List<Graduate>>> getByMajorId(@PathVariable Integer majorId) {
        return ResponseEntity.ok(Result.success(graduateService.getGraduatesByMajorId(majorId)));
    }

    /**
     * 批量导入毕业生
     */
    @PostMapping("/batch")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> batchImport(@RequestBody List<Graduate> graduates) {
        try {
            return ResponseEntity.ok(Result.success(graduateService.batchImport(graduates)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }
} 